package com.xxx.springboot.controller;

import cn.shuibo.annotation.Decrypt;
import cn.shuibo.annotation.Encrypt;
import com.xxx.springboot.listener.SystemListener;
import com.xxx.springboot.pojo.bean.TestBean;
import com.xxx.springboot.pojo.entity.Punish;
import com.xxx.springboot.service.PunishService;
import com.xxx.springboot.util.JsonUtil;
import com.xxx.springboot.util.PythonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * demo Controller
 */
@Controller
@RequestMapping(value = "/", produces = "application/json;charset=utf-8")
public class DemoController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(DemoController.class);
	
	@Resource
	private PunishService punishService;

	// 线程池
	ExecutorService executor = new ThreadPoolExecutor(
			10,
			20,
			10L,
			TimeUnit.MINUTES,
			new ArrayBlockingQueue<>(20),
			new ThreadPoolExecutor.DiscardOldestPolicy());

	/**
	 * 默认模板
	 */
	@RequestMapping("/")
	public String index(){
		return "index";
	}

	/**
     * 系统启动时间戳
     */
    @RequestMapping("/versionNo")
    @ResponseBody
    public String versionNo(){
    	logger.info("enter method versionNo().");
        return SystemListener.versionNo;
    }

	/**
	 * 测试数据库
	 * @return
	 */
	@RequestMapping("/test")
	@ResponseBody
	public String test(){
		logger.info("enter method test().");
		List<Punish> list = punishService.lambdaQuery().last(" limit 10 ").list();
		return JsonUtil.encodeJson(list);
	}

	/**
	 * 测试CompleteFuture
	 * @return
	 */
	@RequestMapping("/testCompleteFuture")
	@ResponseBody
	public String testCompleteFuture(String fundTsCode) throws ExecutionException, InterruptedException {
		logger.info("enter method testCompleteFuture(). fundTsCode:{}", fundTsCode);

		Map<String, String> map = new HashMap<>();

		// 1.调用远程接口获取基金最新净值信息
		CompletableFuture fundNavFuture = CompletableFuture.supplyAsync(
				() -> {
					String nav = PythonUtil.getFundLatestNav(false, fundTsCode);
					logger.info(nav);
					map.put("nav", nav);
					return Boolean.TRUE;
				},
				executor
		);

		// 2.调用远程接口获取基金业绩信息
		CompletableFuture fundAchiveHisFuture = CompletableFuture.supplyAsync(
				() -> {
					String achiveHis = PythonUtil.getFundAchiveHis(false, fundTsCode);
					logger.info(achiveHis);
					map.put("achiveHis", achiveHis);
					return Boolean.TRUE;
				},
				executor
		);

		// 3.调用远程接口获取基金持仓信息
		CompletableFuture fundPosFuture = CompletableFuture.supplyAsync(
				() -> {
					String pos = PythonUtil.getFundPosition(false, fundTsCode);
					logger.info(pos);
					map.put("pos", pos);
					return Boolean.TRUE;
				},
				executor
		);

		fundNavFuture.get();
		fundAchiveHisFuture.get();
		fundPosFuture.get();

		return JsonUtil.encodeJson(map);
	}

	/***
	 * 访问/hello  跳转到demo1页面
	 * 参考：https://blog.csdn.net/qq_20286065/article/details/110432124
	 * @return
	 */
	@RequestMapping("/hello")
	public String hello(Model model){
		model.addAttribute("hello","hello welcome");
		return "demo";
	}

	/**
	 * 测试全局异常处理
	 * @return
	 */
	@RequestMapping("/zeroException")
	public int zeroException(){
		return 100/0;
	}

	/**
	 * 测试RSA加密注解
	 * 参考：https://mp.weixin.qq.com/s/_mINXg18hrlX5lR3IVRdGQ
	 */
	@Encrypt
	@GetMapping("/encryption")
	@ResponseBody
	public TestBean encryption(){
		TestBean testBean = new TestBean();
		testBean.setName("shuibo.cn");
		testBean.setAge(18);
		return testBean;
	}

	@Decrypt
	@PostMapping("/decryption")
	public String Decryption(@RequestBody TestBean testBean){
		return testBean.toString();
	}

}
